﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Agile.Engine.Service
{
    public class DefaultTaskAllocator : TaskAllocator
    {
        public override bool IsRedirect(object args)
        {
            return false;
        }

        public override string ExecuteRedirect(object args)
        {
            return string.Empty;
        }

        public override object Parse(string args)
        {
            var hash = Agile.Engine.Utils.StringUtils.FromJsonBase64<System.Collections.Hashtable>(args);
            return hash;
        }

        public override string ProcessResult(object args)
        {
            return Agile.Engine.Utils.StringUtils.ToJsonBase64(args);
        }

        public override string ProcessException(Exception ex)
        {
            System.Collections.Hashtable model = new System.Collections.Hashtable();
            model["__status"] = -1;
            model["__msg"] = ex.Message;
            return Agile.Engine.Utils.StringUtils.ToJsonBase64(model);
        }

        public override object ExecuteTask(object args)
        {
            System.Collections.Hashtable model = args as System.Collections.Hashtable;

            var context = model["__dataContext"].ToString();
            var rule = model["__dataRule"].ToString();

            Agile.Engine.Data.DataContext dc = Data.DataContext.Get(context);

            //dc.ResetConnString(model["userinfo"] as System.Collections.Hashtable);

            return dc.ExecuteRule(rule, model).Result;
        }
    }
}
